package cn.iocoder.yudao.module.shop.service.shopbrand;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.shop.controller.admin.shopbrand.vo.ShopBrandPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.shopbrand.vo.ShopBrandSaveReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopbrand.ShopBrandDO;
import cn.iocoder.yudao.module.shop.dal.mysql.shopbrand.ShopBrandMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.SHOP_BRAND_NOT_EXISTS;

/**
 * 商户品牌 Service 实现类
 *
 * @author ccc
 */
@Service
@Validated
public class ShopBrandServiceImpl implements ShopBrandService {

    @Resource
    private ShopBrandMapper shopBrandMapper;

    @Override
    public Long createShopBrand(ShopBrandSaveReqVO createReqVO) {
        // 插入
        ShopBrandDO shopBrand = BeanUtils.toBean(createReqVO, ShopBrandDO.class);
        shopBrand.setStatus("01");
        shopBrandMapper.insert(shopBrand);
        // 返回
        return shopBrand.getId();
    }

    @Override
    public void updateShopBrand(ShopBrandSaveReqVO updateReqVO) {
        // 校验存在
        ShopBrandDO dbData = shopBrandMapper.selectById(updateReqVO.getId());
        if (dbData == null) {
            throw exception(SHOP_BRAND_NOT_EXISTS);
        }
        // 更新
        ShopBrandDO updateObj = BeanUtils.toBean(updateReqVO, ShopBrandDO.class);
        shopBrandMapper.updateById(updateObj);
    }

    @Override
    public void deleteShopBrand(Long id) {
        // 校验存在
        validateShopBrandExists(id);
        // 删除
        shopBrandMapper.deleteById(id);
    }

    private void validateShopBrandExists(Long id) {
        if (shopBrandMapper.selectById(id) == null) {
            throw exception(SHOP_BRAND_NOT_EXISTS);
        }
    }

    @Override
    public ShopBrandDO getShopBrand(Long id) {
        return shopBrandMapper.selectById(id);
    }

    @Override
    public PageResult<ShopBrandDO> getShopBrandPage(ShopBrandPageReqVO pageReqVO) {
        return shopBrandMapper.selectPage(pageReqVO);
    }

}